#include	"a_global_vars.h"

//------------------------------------------------------------------------------
int  create_tcp_client_socket(int port, const char *ip){
	struct sockaddr_in server_addr;
	int e = -1;
	int sid;
	memset(&server_addr, '\0', sizeof(struct sockaddr_in));
	
	sid = socket(AF_INET,SOCK_STREAM,0);
	
	server_addr.sin_family=AF_INET;
	server_addr.sin_addr.s_addr=inet_addr(ip);
	server_addr.sin_port=htons(port);
	
	while(e < 0){
		e = connect(sid, (struct sockaddr*)&server_addr, sizeof(struct sockaddr));
		if(e>=0)
		    break;
		close(sid);
		sleep(1);
		sid=socket(AF_INET,SOCK_STREAM,0);
	}
	return sid;
}
//------------------------------------------------------------------------------
int  create_tcp_server_socket(int port, const char *ip)
{
	struct sockaddr_in server_addr, client_addr;
	int sid,e,activate=1, new_sid;
	unsigned int client_addr_len;
	
	client_addr_len = sizeof(client_addr);
	sid = socket(AF_INET,SOCK_STREAM,0);
	setsockopt(sid, SOL_SOCKET, SO_REUSEADDR, &activate, sizeof(int));
	
	server_addr.sin_family=AF_INET;
	server_addr.sin_addr.s_addr=inet_addr(ip);
	server_addr.sin_port=htons(port);
	
	e=bind(sid, (struct sockaddr*)&server_addr, sizeof(struct sockaddr));
	assert(e >= 0);
	e = listen(sid, MAX_RING_CONNECTION);
	
	printf("Server: PID: %d Waiting for connection requests from learners...\n", PROCESS_ID) ;
	new_sid = accept(sid,(struct sockaddr*)&client_addr, &client_addr_len);
	printf("connection request came from the node with port number: %d\n",ntohs(client_addr.sin_port)); 
	
	return new_sid;
}
//------------------------------------------------------------------------------ 
int  server_socket_4_pla(int port, const char *ip, int connectionCount, char pla)
{
	struct sockaddr_in server_addr;
	int sid,e,activate = 1;
	struct sockaddr_in client_addr;
	unsigned int client_addr_len;
	client_addr_len=sizeof(client_addr);
	
	sid = socket(AF_INET,SOCK_STREAM,0);
	setsockopt(sid, SOL_SOCKET, SO_REUSEADDR, &activate, sizeof(int));
	
	server_addr.sin_family=AF_INET;
	server_addr.sin_addr.s_addr=inet_addr(ip);//INADDR_ANY;
	server_addr.sin_port=htons(port);
	e = bind(sid,(struct sockaddr*)&server_addr, sizeof(struct sockaddr));
	assert(e >= 0);
	e = listen(sid,connectionCount);
	assert(e >= 0 );
/*	if(pla=='p')
	  {
		  printf("waiting for the conenction from proposers....\n");
		  while(proposer_connection_cnt < connectionCount)
			{
				proposer_connections[proposer_connection_cnt].new_sid=accept(sid,(struct sockaddr*)&(proposer_connections[proposer_connection_cnt].client_addr), &client_addr_len);
				printf("%d th proposer request arrived\n",(proposer_connection_cnt+1));
				printf("SOCKET used for this new connection:%d\n",proposer_connections[proposer_connection_cnt].new_sid);
				proposer_connection_cnt++; 
			}
	  }
	*/
	if(pla=='l')
	  {
		  printf("waiting for the conenction from learners on: %s..%d..\n", ip,port);
		  while(learner_connection_cnt < connectionCount)
			{
				learner_connections[learner_connection_cnt].new_sid = accept(sid, (struct sockaddr*)&(learner_connections[learner_connection_cnt].client_addr), &client_addr_len);
				printf("%d th learner request arrived\n",(learner_connection_cnt+1));
				printf("SOCKET used for this new connection:%d\n",learner_connections[learner_connection_cnt].new_sid);
				learner_connection_cnt++; 
			}
	  }
	if(pla=='a')
	  {
		  printf("waiting for the conenction from acceptors on: %s..%d..\n", ip,port);
		  while(acceptor_connection_cnt < connectionCount)
			{
				acceptor_connections[acceptor_connection_cnt].new_sid = accept(sid,(struct sockaddr*)&(acceptor_connections[acceptor_connection_cnt].client_addr), &client_addr_len);
				printf("%d th acceptor request arrived\n",(acceptor_connection_cnt+1));
				printf("SOCKET used for this new connection:%d\n",acceptor_connections[acceptor_connection_cnt].new_sid);
				acceptor_connection_cnt++; 
			}
	  }
	return sid;	
}
//-----------------------------------------------------------------------------
